package com.oasistudio.tk.connector.imp;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

public class CrownShopFloors{
	public static final int TIMEOUT = 10000;

	public CrownShopFloors(){
	}
	
	public String getApiUrl() {
		StringBuilder sb = new StringBuilder("http://www.cloudmenu.cn:8080/taobaoke/floors.jsp");
		String url = sb.toString();
		System.out.println("TaobaoShopSearchConnector.getApiUrl() url="+url);
		return url;
	}
	
	public FloorsInfo syncConnect() {
		try {
			long startTime = System.currentTimeMillis();
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet();
			request.setURI(new URI(getApiUrl()));
			HttpResponse response = client.execute(request);
			ByteArrayOutputStream bs = new ByteArrayOutputStream();
			int i = 0;
			DataInputStream dis = new DataInputStream(response.getEntity().getContent());
			byte[] b = new byte[2048];
			while ((i = dis.read(b, 0, 2048)) != -1) {
				// System.out.println("mellon:ApiConnector.syncConnect() "+i+" bytes response data readed!");
				bs.write(b, 0, i);
				if (System.currentTimeMillis() - startTime > TIMEOUT * 4) {
					bs.reset();
					break;
				}
			}
			return this.syncPaser(bs.toByteArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public FloorsInfo syncPaser(byte[] all) {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMinimumFractionDigits(2);
		nf.setMaximumFractionDigits(2);
		FloorsInfo floorsInfo = new FloorsInfo();
		try {
			String str = new String(all, "UTF-8");
			JSONArray floorsJSON = new JSONArray(str);
			if (floorsJSON!=null && floorsJSON.length()>0) {
				floorsInfo.isSuccess = true;
				for (int i = 0; i < floorsJSON.length(); i++) {
					JSONObject crownShopFloor = floorsJSON.getJSONObject(i);
					CrownShopFloor floor = new CrownShopFloor();
					floor.title = crownShopFloor.optString("floor_title");
					JSONArray floor_shops = crownShopFloor.getJSONArray("floor_shops");
					for(int f=0; f<floor_shops.length(); f++){
						JSONObject itemJSON = floor_shops.getJSONObject(f);
						Item item = new Item();
						item.keyword = itemJSON.optString("keyword");
						item.item_id = itemJSON.optLong("item_id");
						item.pic_url = itemJSON.optString("pic_url");
						item.title = itemJSON.optString("title");
						floor.items.add(item);
					}
					floorsInfo.floors.add(floor);
				}
			} else {
				floorsInfo.isSuccess = false;
				//shopsInfo.errCode = response.errCode;
				//shopsInfo.errInfo = response.errInfo;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return floorsInfo;
	}

	public static class FloorsInfo {
		public String errCode;
		public String errInfo;
		public Boolean isSuccess;
		public List<CrownShopFloor> floors = new ArrayList<CrownShopFloor>();
	}
    public static class CrownShopFloor {
    	public String title;
    	public List<Item> items = new ArrayList<Item>();
    }
    public static class Item {
    	public String keyword;
		public String title;
		public String pic_url;
		public Number item_id;
	}
}



